import { lib, game, ui, get, ai, _status } from '../../../noname.js';
import { swTool } from '../main/swTool.js';
lib.skill.autoswap = {
    firstDo: true,
    trigger: {
        player: [
            "playercontrol", "chooseToUseBegin", "chooseToRespondBegin", 
            "chooseToDiscardBegin", "chooseToCompareBegin", "chooseButtonBegin", 
            "chooseCardBegin", "chooseTargetBegin", "chooseCardTargetBegin", 
            "chooseControlBegin", "chooseBoolBegin", "choosePlayerCardBegin", 
            "discardPlayerCardBegin", "gainPlayerCardBegin", "chooseToMoveBegin", 
            "chooseToPlayBeatmapBegin", "chooseToGiveBegin",'chooseSkillBegin',
        ],
    },
    forced: true,
    priority: 100,
    forceDie: true,
    popup: false,
    charlotte:true,
    filter(event, player) {
        if(!player._controlNow&&!player._trueMe) return false
        if (lib.filter.wuxieSwap(event)) return false;
        const huoxinNow = "惑心："+get.translation(player)
        if(player._trueMe) {//如果要操作的角色不是自己且有真正的操作者那就符合
            return player._trueMe.playerControlNow!== huoxinNow
        } 
        if(player.playerControlNow!==huoxinNow) {//如果现在要操作的是自己
            return  true
        }
        return true;
    },
    async content(event,trigger,player) {//当时机触发时，拥有这个切换的玩家会切换到自己身上
        const huoxinNow = "惑心："+get.translation(player)
        let source,target;
        if(player._trueMe&&player._trueMe.playerControlNow!==huoxinNow){
            player._trueMe.playerControlNow = huoxinNow
            source = player._trueMe
            target = player
        }else if(player._controlNow&&player.playerControlNow!==huoxinNow){
            player.playerControlNow = huoxinNow
            source = player
            target = player._controlNow
        }
        if(_status.connectMode&&source&&target){
            swTool.swapPlayer({
                player:source,
                player2:target,
                unidirectional:true
            })
        }else {
            game.swapPlayerAuto(player);
        }
        
    },
}
lib.skill.huoxin_control = {
    audio: "huoxin",
    forced: true,
    trigger: { global: "phaseBeginStart" },
    filter(event, player) {
        return player != event.player && !event.player._trueMe && event.player.countMark("huoxin") > 1;
    },
    logTarget: "player",
    skillAnimation: true,
    animationColor: "metal",
    async content(event,trigger,player) {
        trigger.player.removeMark("huoxin", trigger.player.countMark("huoxin"));
        trigger.player._trueMe = player;
        if(trigger.player===game.me){
            game.me.mainHost = {mainHost:'mainHost',nickname:game.me.nickname}
            swTool.observingId.push(game.me.mainHost)
            game.notMe = true
            _status.auto = true
            game.me.chat = function(str){
                lib.message.server.chat.call(game.me.mainHost,null,str)
            }
        }else {
            swTool.observingId.push(trigger.player.ws)
        }
        
        if(_status.connectMode){
            trigger.player.tempNickname = player.nickname
        }

        game.addGlobalSkill('autoswap')//给自己加切换
        trigger.player.addTempSkill("huoxin2", 'phaseAfter');

        const source = player, target = trigger.player
        game.log('#r',get.translation(source)+"已经控制了"+get.translation(target))
    },
},
lib.skill.huoxin2 = {
    trigger: {
        player: ["phaseAfter", "dieAfter"],
        global: "phaseBeforeStart",
    },
    lastDo: true,
    charlotte: true,
    forceDie: true,
    forced: true,
    silent: true,
    async content(e,t,player) {
        player.removeSkill("huoxin2");
    },
    onremove(player) {
        if (player == game.me) {
            delete game.notMe
            delete _status.auto
            swTool.observingId.remove(game.me.mainHost)
            player.chat = lib.element.player.chat.bind(player)
        }else {
            swTool.observingId.remove(player.ws)
        }
        const huoxinNow = "惑心："+get.translation(player)
        if(player._trueMe.playerControlNow===huoxinNow){
            swTool.swapPlayer({
                player:player._trueMe,
                player2:player,
                unidirectional:true
            })   
            player._trueMe.playerControlNow = huoxinNow
        }
        delete player.tempNickname
        delete player._trueMe;
        game.removeGlobalSkill('autoswap')
    },
}
